import json
import undetected_chromedriver as uc
import json
from lxml import etree
import pymysql
from db.mysql.db_entity_stock_ths_gn import ThsGn
from db.mysql.db_mysql_insert import addStockNews
from db.mysql.db_entity_stock_news import StockNews

with open('../test2.txt', 'r', encoding='utf-8') as file:
    content = file.read()

# print(content)
html = etree.fromstring(content,etree.HTMLParser())
# result = html.xpath('//div[@class="left"]//ul//a//text()')
# result = html.xpath('//div[@class="left"]//ul//div[@class="share-popup"]//@data-url')
urls = html.xpath('//div[contains(@class,"share-popup")]//@data-url')
for item in urls:
    print(item)

titles = html.xpath('//div[contains(@class,"share-popup")]//@data-title')
for item in titles:
    print(item)

# 多个class 选择器
descs = html.xpath('//div[contains(@class,"share-popup")]//@data-description')
for item in descs:
    print(item)

newsTimes = html.xpath('//ul//div[@class="time"]//text()')
for item in newsTimes:
    print(item)

print("================================================")
print(urls[0])

db = pymysql.connect(host='39.98.82.109',user='root',password='landleaf@123.com',port=3306,db='stock')
sql = 'INSERT INTO stock_news (title, content,channel,url) VALUES (%s, %s,%s,%s)'
for index,item in enumerate(urls):
    print(titles[index])
    stockNews = StockNews(titles[index],descs[index],'e网',urls[index])
    addStockNews(db,sql,stockNews)

db.close()




# 获取 a 下面的链接地址
# result2 = html.xpath('//tbody//a/@href')
# for item in result2:
#     print(item)